﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace HigLabo.Net.Twitter
{
    public partial class TwitterClient
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public Friends GetFollowers(Int64 userID)
        {
            var cm = new GetFriendsCommand();
            cm.UserID = userID;
            return this.GetFollowers(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Friends GetFollowers(GetFriendsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetFollowersUrl, command);
            return new Friends(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public Friends GetFriends(Int64 userID)
        {
            var cm = new GetFriendsCommand();
            cm.UserID = userID;
            return this.GetFriends(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Friends GetFriends(GetFriendsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetFriendsUrl, command);
            return new Friends(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Friends GetIncomingFriends()
        {
            return this.GetIncomingFriends(null as GetPendingFollowFriendsCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Friends GetIncomingFriends(GetPendingFollowFriendsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetIncomingUrl, command);
            return new Friends(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Friends GetOutgoingFriends()
        {
            return this.GetOutgoingFriends(null as GetPendingFollowFriendsCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Friends GetOutgoingFriends(GetPendingFollowFriendsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetOutgoingUrl, command);
            return new Friends(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceID"></param>
        /// <param name="targetID"></param>
        /// <returns></returns>
        public Relationship ShowFriendships(Int64 sourceID, Int64 targetID)
        {
            var cm = new ShowFriendshipsCommand();
            cm.SourceID = sourceID;
            cm.TargetID = targetID;
            return this.ShowFriendships(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceScreenName"></param>
        /// <param name="targetScreenName"></param>
        /// <returns></returns>
        public Relationship ShowFriendships(String sourceScreenName, String targetScreenName)
        {
            var cm = new ShowFriendshipsCommand();
            cm.SourceScreenName = sourceScreenName;
            cm.TargetScreenName = targetScreenName;
            return this.ShowFriendships(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Relationship ShowFriendships(ShowFriendshipsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.ShowUrl, command);
            return new Relationship(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public User DestroyFriendship(Int64 userID)
        {
            var cm = new DestroyFriendshipCommand();
            cm.UserID = userID;
            return this.DestroyFriendship(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="screenName"></param>
        /// <returns></returns>
        public User DestroyFriendship(String screenName)
        {
            var cm = new DestroyFriendshipCommand();
            cm.ScreenName = screenName;
            return this.DestroyFriendship(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public User DestroyFriendship(DestroyFriendshipCommand command)
        {
            String jsonText = this.GetResultText(HttpMethodName.Post, TwitterApiUrl.Version1_1.Friends.DestroyUrl, command);
            return new User(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public User CreateFriendship(Int64 userID)
        {
            var cm = new CreateFriendshipCommand();
            cm.UserID = userID;
            return this.CreateFriendship(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="screenName"></param>
        /// <returns></returns>
        public User CreateFriendship(String screenName)
        {
            var cm = new CreateFriendshipCommand();
            cm.ScreenName = screenName;
            return this.CreateFriendship(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public User CreateFriendship(CreateFriendshipCommand command)
        {
            String jsonText = this.GetResultText(HttpMethodName.Post, TwitterApiUrl.Version1_1.Friends.CreateUrl, command);
            return new User(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public Relationship UpdateFriendship(UpdateFriendshipCommand command)
        {
            String jsonText = this.GetResultText(HttpMethodName.Post, TwitterApiUrl.Version1_1.Friends.UpdateUrl, command);
            return new Relationship(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Int64> GetNoRetweetIDs()
        {
            return this.GetNoRetweetIDs(null as GetNoRetweetIDsCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public IEnumerable<Int64> GetNoRetweetIDs(GetNoRetweetIDsCommand command)
        {
            List<Int64> result = new List<Int64>();
            String jsonText = this.GetResultText (HttpMethodName.Get, TwitterApiUrl.Version1_1.Friends.GetNoRetweetIDsUrl, command);
            var cc = JsonConvert.DeserializeObject(jsonText) as JContainer;
            if (cc != null)
            {
                foreach (var i in cc)
                {
                    result.Add(Convert.ToInt64(i.ToString()));
                }
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uidValues"></param>
        /// <returns></returns>
        public FriendshipCollection LookupFriendships(params Int64[] uidValues)
        {
            StringBuilder sb = new StringBuilder(256);
            for (int i = 0; i < uidValues.Length; i++)
            {
                sb.Append(uidValues[i]);
                if (i < uidValues.Length - 1)
                {
                    sb.Append(",");
                }
            }
            var cm = new LookupFriendshipsCommand();
            cm.UserID = sb.ToString();
            return this.LookupFriendships(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nameValues"></param>
        /// <returns></returns>
        public FriendshipCollection LookupFriendships(params String[] nameValues)
        {
            StringBuilder sb = new StringBuilder(256);
            for (int i = 0; i < nameValues.Length; i++)
            {
                sb.Append(nameValues[i]);
                if (i < nameValues.Length - 1)
                {
                    sb.Append(",");
                }
            }
            var cm = new LookupFriendshipsCommand();
            cm.ScreenName = sb.ToString();
            return this.LookupFriendships(cm);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public FriendshipCollection LookupFriendships(LookupFriendshipsCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetLookupUrl, command);
            return new FriendshipCollection(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public FriendsUser GetFriendsList()
        {
            return GetFriendsList(null as GetFriendsUserCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public FriendsUser GetFriendsList(GetFriendsUserCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetFriendsListUrl, command);
            return new FriendsUser(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public FriendsUser GetFollowerList()
        {
            return GetFollowerList(null as GetFriendsUserCommand);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public FriendsUser GetFollowerList(GetFriendsUserCommand command)
        {
            String jsonText = this.GetResultText(TwitterApiUrl.Version1_1.Friends.GetFollowerListUrl, command);
            return new FriendsUser(jsonText);
        }
    }
}
